#ifndef __PAGE_H__
#define __PAGE_H__

#include <vector>
#include "gofgame/common/ArgumentException.h"
#include "gofgame/common/cxx_0x.h"
using namespace std;
using namespace gofgame::common;

namespace gofgame {
	namespace jumpdown {
		namespace io {
			template <typename E>
			class Page {
			public:
				Page(int pageIndex, int pageSize, int totalRowCount, const vector<E> &entities) {
					if (pageSize < 1) {
						_throw(ArgumentException, "pageSize must >= 1");
					}
					if (pageIndex < 0) {
						_throw(ArgumentException, "pageIndex > 0");
					}
					this->totalRowCount = totalRowCount;
					this->pageCount = (totalRowCount + pageSize - 1) / pageSize;
					this->pageSize = pageSize;
					this->expectedPageIndex = pageIndex;
					this->actualPageIndex = _max(0, _min(pageIndex, pageCount - 1));
					for (E entity : entities) {
						this->entities.push_back(entity);
					}
				}

				int getTotalRowCount() const { return this->totalRowCount; }

				int getPageCount() const { return this->pageCount; }

				int getPageSize() const { return this->pageSize; }

				int getExpectedPageIndex() const { return this->expectedPageIndex; }

				int getActualPageIndex() const { this->actualPageIndex; }

				int count() const { return this->entities.size(); }

				E operator[] (int index) const { return this->entities[index]; }
				
			private:
				int totalRowCount;
				int pageCount;
				int pageSize;
				int expectedPageIndex;
				int actualPageIndex;
				vector<E> entities;
			};
		}
	}
}

#endif //__PAGE_H__
